package com.example.springbootbook2.config;

import com.example.springbootbook2.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
@ResponseBody
public class ErrorHandler {
    @ExceptionHandler
    public Result<String> exception(Exception e) {
        log.error("发生异常:e{}",e);
        return Result.fail("内部异常");
    }

    @ExceptionHandler
    public Result<String> exception(NullPointerException e) {
        log.error("发生异常:e{}",e);
        return Result.fail("NullPointerException异常，请联系管理员");
    }

    @ExceptionHandler
    public Result<String> exception(ArithmeticException e) {
        log.error("发生异常:e{}",e);
        return Result.fail("ArithmeticException异常，请联系管理员");
    }
}
